package com.yanqu.road.dao.impl.union.cross.huaben;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.huaben.UnionHuaBenMemberWeekData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionHuaBenMemberWeekDataDaoImpl extends TempDataObjectDao<UnionHuaBenMemberWeekData> {


    public static String getUnionHuaBenMemberWeekDataSql = "select * from `t_c_hua_ben_member_week` where `weekId`=? or `weekId` =?";
    public Map<Integer, Map<Long, Map<Long, UnionHuaBenMemberWeekData>>> getUnionHuaBenMemberWeekData(int preWeekId, int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, Map<Long, UnionHuaBenMemberWeekData>>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionHuaBenMemberWeekDataSql);
            int i = 1;
            call.setLong(i++, preWeekId);
            call.setLong(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHuaBenMemberWeekData weekData = parse(rs);
                if(result.get(weekData.getWeekId()) == null){
                    result.put(weekData.getWeekId(), new ConcurrentHashMap<>());
                }
                Map<Long, Map<Long, UnionHuaBenMemberWeekData>> weekMap = result.get(weekData.getWeekId());

                if(weekMap.get(weekData.getServerId()) == null){
                    weekMap.put(weekData.getServerId(), new ConcurrentHashMap<>());
                }
                Map<Long, UnionHuaBenMemberWeekData> serverMap = weekMap.get(weekData.getServerId());
                serverMap.put(weekData.getUserId(),weekData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHuaBenMemberWeekDataSql = "insert into `t_c_hua_ben_member_week`(userId,weekId,serverId,unionUid,canSendTimes,sendTimes,resetTime,score,settleScore,userBaseInfo,settleSync) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionHuaBenMemberWeekData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCanSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSettleScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserBaseInfoJsonString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSettleSync()));
        if (getDb().executeNoneQuery(insertUnionHuaBenMemberWeekDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionHuaBenMemberWeekDataSql = "update `t_c_hua_ben_member_week` set serverId=?,unionUid=?,canSendTimes=?,sendTimes=?,resetTime=?,score=?,settleScore=?,userBaseInfo=?,settleSync=? where userId=? and weekId=?";

    @Override
    protected boolean updateData(UnionHuaBenMemberWeekData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCanSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSettleScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserBaseInfoJsonString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSettleSync()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        if (getDb().executeNoneQuery(updateUnionHuaBenMemberWeekDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionHuaBenMemberWeekData parseData(ResultSet rs) throws SQLException {
        UnionHuaBenMemberWeekData data = new UnionHuaBenMemberWeekData();
        data.setUserId(rs.getLong("userId"));
        data.setWeekId(rs.getInt("weekId"));
        data.setServerId(rs.getLong("serverId"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setCanSendTimes(rs.getInt("canSendTimes"));
        data.setSendTimes(rs.getInt("sendTimes"));
        data.setResetTime(rs.getInt("resetTime"));
        data.setScore(rs.getInt("score"));
        data.setSettleScore(rs.getInt("settleScore"));
        String value = rs.getString("userBaseInfo");
        if(!StringUtils.isNullOrEmpty(value)){
            UserBaseInfo userBaseInfo = JSON.parseObject(value, new TypeReference<UserBaseInfo>() {});
            if(null != userBaseInfo) {
                data.setUserBaseInfo(userBaseInfo);
            }
        }
        data.setSettleSync(rs.getBoolean("settleSync"));
        return data;
    }
}
